# coding=utf-8
# -*- coding: UTF-8 -*-
#
# This file is part of the kernelstudio package.
#
# (c) 2014-2025 zlin <admin@kernelstudio.com>
#
# For the full copyright and license information, please view the LICENSE file
# that was distributed with this source code.
import typing as t
from enum import StrEnum

from ksai.persistence.database import db
from ksai.persistence.storage.attachment import Attachment
from ksai.query import Query
from ksai.serializer import JsonSerializable


class DocumentStatus(StrEnum):
    UPLOADING = 'uploading'
    UPLOADED = "uploaded"
    LABELING = "labeling"
    EXTRACTING = "extracting"
    EXTRACTED = "extracted"
    LOCKING = "locking"
    LOCKED = "locked"


class DocumentAttachmentModelQuery(Query):

    def __init__(self):
        super().__init__()


class DocumentAttachmentModel(db.Model, JsonSerializable):
    __tablename__ = 'ksai_document_attachment'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    uuid = db.Column(db.String(60), unique=True, nullable=False)
    category = db.Column(db.String(60), nullable=True)
    filepath = db.Column(db.String(255), nullable=True)
    filename = db.Column(db.String(255), nullable=True)
    original_name = db.Column(db.String(255), nullable=True)
    mime = db.Column(db.String(255), nullable=True)
    size = db.Column(db.String(10), nullable=True)
    suffix = db.Column(db.String(10), nullable=True)
    status = db.Column(db.String(20), nullable=True, default="uploading")
    created_at = db.Column(db.DateTime(timezone=True), default=None, nullable=True)
    updated_at = db.Column(db.DateTime(timezone=True), default=None, nullable=True)
    deleted_at = db.Column(db.DateTime(timezone=True), default=None, nullable=True)

    def serialize(self) -> t.Optional[dict]:
        return {
            "uuid": self.uuid,
            "category": self.category,
            "filepath": self.filepath,
            "filename": self.filename,
            "original_name": self.original_name,
            "mime": self.mime,
            "size": self.size,
            "suffix": self.suffix,
            "status": self.status,
            "created_at": self.created_at,
            "updated_at": self.updated_at
        }


class DocumentContext:
    attachment: Attachment
    document: DocumentAttachmentModel

    def __init__(self, attachment: Attachment, document: DocumentAttachmentModel):
        self.attachment = attachment
        self.document = document
